package cn.lingyangwl.agile.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.lingyangwl.agile.system.mapper.*;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 表单字段
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class DiyFormFieldServiceImpl extends ServiceImpl<DiyFormFieldMapper, DiyFormField>
    implements IService<DiyFormField>, DiyFormFieldService {

    @Override
    public List<DiyFormField> listField(List<Long> formIds) {
        if (CollUtil.isEmpty(formIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DiyFormField> lqw = DiyFormField.lqw().in(DiyFormField::getFormId, formIds);
        return this.list(lqw);
    }

    @Override
    public void deleteField(List<Long> formId, List<Long> fieldIds) {
        if (CollUtil.isEmpty(formId) && CollUtil.isEmpty(fieldIds)) {
            return;
        }
        LambdaQueryWrapper<DiyFormField> lqw = DiyFormField.lqw()
            .in(CollUtil.isNotEmpty(formId), DiyFormField::getFormId, formId)
            .in(CollUtil.isNotEmpty(fieldIds), DiyFormField::getId, fieldIds);
        this.remove(lqw);
    }

    @Override
    public void saveFields(DiyForm entity, List<DiyFormField> fieldList) {
        fieldList.forEach(e -> {
            e.setId(null);
            e.setFormId(entity.getId());
        });

        LambdaQueryWrapper<DiyFormField> lqw = DiyFormField.lqw()
            .eq(DiyFormField::getFormId, entity.getId());
        this.remove(lqw);
        this.saveBatch(fieldList);

    }
}
